﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using TaobaoDistrSales.Model;
using System.Windows.Forms;
using System.Data.Objects;

namespace TaobaoDistrSales.Common
{
    public class ResourceManager
    {
        #region Static Ctor

        static ResourceManager()
        {
            bool useTop = true;
            string useTopValue = ConfigurationManager.AppSettings[Constants.UseTopKey];
            if (!String.IsNullOrEmpty(useTopValue))
            {
                Boolean.TryParse(useTopValue, out useTop);
            }
            
            string urlKey = useTop ? Constants.TopUrlKey:Constants.SandBoxUrlKey;

            Current.RequestUrl = ConfigurationManager.AppSettings[urlKey]; 

            if (String.IsNullOrEmpty(Current.RequestUrl))
            {
                Current.RequestUrl = Constants.TopUrl;
                //throw new DistrSalesException("系统没有配置好淘宝API的URL!");
            }

            Current.ConnectionString = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings[Constants.ConnectionStrName]].ConnectionString;
             
            Current.OptionsConfig = LoadConfig();
        }

        protected ResourceManager()
        { 
        }

        #endregion

        #region Current

        public static readonly ResourceManager Current = new ResourceManager();

        #endregion

        #region Properties

        public string RequestUrl
        {
            get;
            private set;
        }

        protected string ConnectionString
        {
            get;
            private set;
        }

        private OptionsConfig optionsConfig = null;
        public OptionsConfig OptionsConfig
        {
            get
            {
                if (optionsConfig == null)
                {
                    optionsConfig = LoadConfig();
                }
                return optionsConfig;
            }
            set
            {
                optionsConfig = value;
            }
        }
        #endregion

        #region Load

        public static taobaodbEntities  LoadEntity()
        {
            taobaodbEntities entities = null;
            try
            {
                entities = new taobaodbEntities(Current.ConnectionString);
               
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return entities;
        }

        public static OptionsConfig LoadConfig()
        {
            OptionsConfig optionsConfig = null;
            using (taobaodbEntities entities = LoadEntity())
            {
                optionsConfig = (from o in entities.OptionsConfig select o).FirstOrDefault();
                if (optionsConfig == null)
                {
                    optionsConfig = new OptionsConfig();
                    optionsConfig.displaypanelnum = 1;
                    optionsConfig.pagesize = 10;
                    optionsConfig.enablefont = true;
                    optionsConfig.openpanelatstart = false;
                    optionsConfig.processinterval = 10;
                    optionsConfig.clearoutput = true;
                    optionsConfig.showlog = true;
                    optionsConfig.showmax = false;
                    optionsConfig.traderatecount = 1000;
                    entities.AddToOptionsConfig(optionsConfig);
                    entities.SaveChanges();
                }
            }
            return optionsConfig;
        }

        public void DisposeEntity(ObjectContext entity)
        {
            try
            {
                if (entity != null)
                {
                    entity.Dispose();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        #endregion
    }
}
